<cfcomponent output="false">

	<cffunction name="read" output="false" access="public" returntype="slr.components.cfgenerated.Instituicao_Detail.InstituicaoBean">
		<cfargument name="id" required="true">
		<cfset var qRead="">
		<cfset var obj="">

		<cfquery name="qRead" datasource="BRUST">
			select 	institucaoId, nome, enderecoLogradouro, enderecoBairro, enderecoCidade, enderecoUF, 
					responsavel, responsavelCRM, dataCadastro, tecnicoCadastro, dataAlteracao, 
					tecnicoAlteracao
			from dbo.Instituicao
			where institucaoId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.id#" />
		</cfquery>

		<cfscript>
			obj = createObject("component", "slr.components.cfgenerated.Instituicao_Detail.InstituicaoBean").init();
			obj.setinstitucaoId(qRead.institucaoId);
			obj.setnome(qRead.nome);
			obj.setenderecoLogradouro(qRead.enderecoLogradouro);
			obj.setenderecoBairro(qRead.enderecoBairro);
			obj.setenderecoCidade(qRead.enderecoCidade);
			obj.setenderecoUF(qRead.enderecoUF);
			obj.setresponsavel(qRead.responsavel);
			obj.setresponsavelCRM(qRead.responsavelCRM);
			obj.setdataCadastro(qRead.dataCadastro);
			obj.settecnicoCadastro(qRead.tecnicoCadastro);
			obj.setdataAlteracao(qRead.dataAlteracao);
			obj.settecnicoAlteracao(qRead.tecnicoAlteracao);
			obj.setEquipamentoBeans(createObject("component", "EquipamentoGateway").getAll(institucaoId=qRead.institucaoId));
			return obj;
		</cfscript>
	</cffunction>



	<cffunction name="create" output="false" access="public" returntype="void">
		<cfargument name="bean" required="true" type="slr.components.cfgenerated.Instituicao_Detail.InstituicaoBean">
		<cfset var qCreate="">

		<cfset var qGetId="">

		<cfset var local1=arguments.bean.getnome()>
		<cfset var local2=arguments.bean.getenderecoLogradouro()>
		<cfset var local3=arguments.bean.getenderecoBairro()>
		<cfset var local4=arguments.bean.getenderecoCidade()>
		<cfset var local5=arguments.bean.getenderecoUF()>
		<cfset var local6=arguments.bean.getresponsavel()>
		<cfset var local7=arguments.bean.getresponsavelCRM()>
		<cfset var local8=arguments.bean.getdataCadastro()>
		<cfset var local9=arguments.bean.gettecnicoCadastro()>
		<cfset var local10=arguments.bean.getdataAlteracao()>
		<cfset var local11=arguments.bean.gettecnicoAlteracao()>

		<cftransaction isolation="read_committed">
			<cfquery name="qCreate" datasource="BRUST">
				insert into dbo.Instituicao(nome, enderecoLogradouro, enderecoBairro, enderecoCidade, enderecoUF, responsavel, responsavelCRM, dataCadastro, tecnicoCadastro, dataAlteracao, tecnicoAlteracao)
				values (
					<cfqueryparam value="#local1#" cfsqltype="CF_SQL_VARCHAR" />,
					<cfqueryparam value="#local2#" cfsqltype="CF_SQL_VARCHAR" />,
					<cfqueryparam value="#local3#" cfsqltype="CF_SQL_VARCHAR" />,
					<cfqueryparam value="#local4#" cfsqltype="CF_SQL_VARCHAR" />,
					<cfqueryparam value="#local5#" cfsqltype="CF_SQL_CHAR" />,
					<cfqueryparam value="#local6#" cfsqltype="CF_SQL_VARCHAR" />,
					<cfqueryparam value="#local7#" cfsqltype="CF_SQL_VARCHAR" />,
					<cfqueryparam value="#local8#" cfsqltype="CF_SQL_TIMESTAMP" null="#iif((local8 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local9#" cfsqltype="CF_SQL_CHAR" />,
					<cfqueryparam value="#local10#" cfsqltype="CF_SQL_TIMESTAMP" null="#iif((local10 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local11#" cfsqltype="CF_SQL_VARCHAR" />
				)
			</cfquery>

			<!--- If your server has a better way to get the ID that is more reliable, use that instead --->
			<cfquery name="qGetID" datasource="BRUST">
				select institucaoId
				from dbo.Instituicao
				where nome = <cfqueryparam value="#local1#" cfsqltype="CF_SQL_VARCHAR" />
				  and enderecoLogradouro = <cfqueryparam value="#local2#" cfsqltype="CF_SQL_VARCHAR" />
				  and enderecoBairro = <cfqueryparam value="#local3#" cfsqltype="CF_SQL_VARCHAR" />
				  and enderecoCidade = <cfqueryparam value="#local4#" cfsqltype="CF_SQL_VARCHAR" />
				  and enderecoUF = <cfqueryparam value="#local5#" cfsqltype="CF_SQL_CHAR" />
				  and responsavel = <cfqueryparam value="#local6#" cfsqltype="CF_SQL_VARCHAR" />
				  and responsavelCRM = <cfqueryparam value="#local7#" cfsqltype="CF_SQL_VARCHAR" />
				  and dataCadastro = <cfqueryparam value="#local8#" cfsqltype="CF_SQL_TIMESTAMP" null="#iif((local8 eq ""), de("yes"), de("no"))#" />
				  and tecnicoCadastro = <cfqueryparam value="#local9#" cfsqltype="CF_SQL_CHAR" />
				  and dataAlteracao = <cfqueryparam value="#local10#" cfsqltype="CF_SQL_TIMESTAMP" null="#iif((local10 eq ""), de("yes"), de("no"))#" />
				  and tecnicoAlteracao = <cfqueryparam value="#local11#" cfsqltype="CF_SQL_VARCHAR" />
				order by institucaoId desc
			</cfquery>
		</cftransaction>

		<cfscript>
			arguments.bean.setinstitucaoId(qGetID.institucaoId);
		</cfscript>

		<cfscript>
			EquipamentoBeans = arguments.bean.getEquipamentoBeans();
			EquipamentoDao = createObject("component", "EquipamentoDAO");

			for (i = 1; i LTE ArrayLen(EquipamentoBeans); i = i + 1)
			{
				EquipamentoBeans[i].setinstitucaoId(arguments.bean.getinstitucaoId());
				EquipamentoDao.create(EquipamentoBeans[i]);
			}
		</cfscript>
	</cffunction>



	<cffunction name="update" output="false" access="public" returntype="void">
		<cfargument name="bean" required="true" type="slr.components.cfgenerated.Instituicao_Detail.InstituicaoBean">
		<cfset var qUpdate="">

		<cftransaction isolation="read_committed">
			<cfquery name="qUpdate" datasource="BRUST" result="status">
				update dbo.Instituicao
				set nome = <cfqueryparam value="#arguments.bean.getnome()#" cfsqltype="CF_SQL_VARCHAR" />,
					enderecoLogradouro = <cfqueryparam value="#arguments.bean.getenderecoLogradouro()#" cfsqltype="CF_SQL_VARCHAR" />,
					enderecoBairro = <cfqueryparam value="#arguments.bean.getenderecoBairro()#" cfsqltype="CF_SQL_VARCHAR" />,
					enderecoCidade = <cfqueryparam value="#arguments.bean.getenderecoCidade()#" cfsqltype="CF_SQL_VARCHAR" />,
					enderecoUF = <cfqueryparam value="#arguments.bean.getenderecoUF()#" cfsqltype="CF_SQL_CHAR" />,
					responsavel = <cfqueryparam value="#arguments.bean.getresponsavel()#" cfsqltype="CF_SQL_VARCHAR" />,
					responsavelCRM = <cfqueryparam value="#arguments.bean.getresponsavelCRM()#" cfsqltype="CF_SQL_VARCHAR" />,
					dataCadastro = <cfqueryparam value="#arguments.bean.getdataCadastro()#" cfsqltype="CF_SQL_TIMESTAMP" null="#iif((arguments.bean.getdataCadastro() eq ""), de("yes"), de("no"))#" />,
					tecnicoCadastro = <cfqueryparam value="#arguments.bean.gettecnicoCadastro()#" cfsqltype="CF_SQL_CHAR" />,
					dataAlteracao = <cfqueryparam value="#arguments.bean.getdataAlteracao()#" cfsqltype="CF_SQL_TIMESTAMP" null="#iif((arguments.bean.getdataAlteracao() eq ""), de("yes"), de("no"))#" />,
					tecnicoAlteracao = <cfqueryparam value="#arguments.bean.gettecnicoAlteracao()#" cfsqltype="CF_SQL_VARCHAR" />
				where institucaoId = <cfqueryparam value="#arguments.bean.getinstitucaoId()#" cfsqltype="CF_SQL_INTEGER">
			</cfquery>

			<cfscript>
				// Update all Child Component properties.
				EquipamentoBeans = arguments.bean.getEquipamentoBeans();
				EquipamentoDao = createObject("component", "EquipamentoDAO");
				EquipamentoGateway = createObject("component", "EquipamentoGateway");

				// return all of the primary keys (linked to this object) in the database.
				oldequipamentoIds = EquipamentoGateway.getAllAsQuery(fieldlist="equipamentoId", institucaoId=arguments.bean.getinstitucaoId());

				beansToDelete = ArrayNew(1);

				// compare the current item ids with the ids pulled out of the database.
				// to find the items that should be deleted.
				for (j = 1; j LTE oldequipamentoIds.recordCount; j = j + 1)
				{
					found = false;
					for (i = ArrayLen(EquipamentoBeans); i GTE 1; i = i - 1)
					{
					lookupBean = EquipamentoBeans[i];
						if (oldequipamentoIds.equipamentoId[j] EQ lookupBean.getequipamentoId())
						{
							found = true;
							break;
						}
					}
					
					// if we find it in the database, but not in the array, then it was deleted
					if (found EQ false)
					{
						ArrayAppend(beansToDelete, EquipamentoDao.read(oldequipamentoIds.equipamentoId[j]));
					}
				}
				
				// delete the ones that are in the to be deleted array
				for (i = 1; i LTE ArrayLen(beansToDelete); i = i + 1)
				{
					EquipamentoDao.delete(beansToDelete[i]);
				}
				
				// update or save the ones that are left
				for (i = 1; i LTE ArrayLen(EquipamentoBeans); i = i + 1)
				{
					EquipamentoBeans[i].setinstitucaoId(arguments.bean.getinstitucaoId());
					EquipamentoGateway.save(EquipamentoBeans[i]);
				}
			</cfscript>
		</cftransaction>
	</cffunction>



	<cffunction name="delete" output="false" access="public" returntype="void">
		<cfargument name="bean" required="true" type="slr.components.cfgenerated.Instituicao_Detail.InstituicaoBean">
		<cfset var qDelete="">

		<cftransaction isolation="read_committed">
			<cfscript>
				EquipamentoBeans = arguments.bean.getEquipamentoBeans();
				EquipamentoDao = createObject("component", "EquipamentoDAO");

				for (i = 1; i LTE ArrayLen(EquipamentoBeans); i = i + 1)
				{
					EquipamentoDao.delete(EquipamentoBeans[i]);
				}
			</cfscript>

			<cfquery name="qDelete" datasource="BRUST" result="status">
				delete
				from dbo.Instituicao
				where institucaoId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.bean.getinstitucaoId()#" />
			</cfquery>
		</cftransaction>

	</cffunction>


</cfcomponent>